package com.book.app.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.book.app.aop.SystemControllerLog;
import com.book.app.entity.Category;
import com.book.app.entity.Student;
import com.book.app.service.StudentService;
import com.book.app.tools.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "学生模块")
@RestController
@RequestMapping("student")
@CrossOrigin //前后端分离配置跨域访问
public class StudentController  {
    /**
     * 服务对象
     */
    @Resource
    private StudentService studentService;

    @ApiOperation("查询所有读者信息，初始化下拉列表")
    @SystemControllerLog(description ="查询所有读者信息，初始化下拉列表")
    @GetMapping("/findAll")
    public CommonResult findAll(){
        return CommonResult.success(studentService.list());
    }

    @ApiOperation("分页查询所有读者信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "分页起始数据",required = true,example = "1"),
            @ApiImplicitParam(name = "limit",value = "每页显示分页最大数据",required = true,example = "10"),
            @ApiImplicitParam(name = "snum",value = "学号查询",example = "20181001"),
            @ApiImplicitParam(name = "sname",value = "姓名查询",example = "王婧莹"),
    })
    @SystemControllerLog(description ="分页查询学生信息")
    @GetMapping("/findPager")
    public CommonResult findPager(Integer page, Integer limit,
                                        String snum, String sname){
        QueryWrapper<Student> queryWrapper=new QueryWrapper<>();//封装条件查询的对象
        //根据学号进行条件查询
        if(!"".equals(snum)&&snum!=null){
            queryWrapper.like(true,"SNUM",snum);
        }
        //根据姓名进行条件查询
        if(!"".equals(sname)&&sname!=null){
            queryWrapper.like(true,"SNAME",sname);
        }
        Map<String,Object>  map=new HashMap<>();
        map.put("page",page);
        map.put("count",studentService.count(queryWrapper));
        map.put("data",studentService.page(new Page<Student>(page,limit),queryWrapper).getRecords());
        return CommonResult.success(map);
    }

    @ApiOperation("添加学生信息")
    @SystemControllerLog(description ="添加学生信息")
    @PostMapping("/save")
    public  CommonResult save(@RequestBody Student student){
        boolean flag =  studentService.save(student);
        return flag?CommonResult.success(flag):CommonResult.failed();
    }

    @ApiOperation("更新学生信息")
    @SystemControllerLog(description ="更新学生信息")
    @PostMapping("/update")
    public  CommonResult update(@RequestBody Student student){
        boolean flag =  studentService.updateById(student);
        return flag?CommonResult.success(flag):CommonResult.failed();
    }

    @ApiOperation("批量删除学生信息")
    @SystemControllerLog(description ="批量删除学生信息")
    @PostMapping("/removeBatch")
    public  CommonResult removeBatch(@RequestParam("sids[]") List<Integer> sids){
        boolean flag = studentService.removeByIds(sids);
        return flag?CommonResult.success(flag):CommonResult.failed();
    }

    @ApiOperation("根据ID删除学生信息")
    @SystemControllerLog(description ="根据ID删除学生信息")
    @PostMapping("/remove")
    public  CommonResult remove(@RequestParam("sid") Integer sid){
        boolean flag = studentService.removeById(sid);
        return flag?CommonResult.success(flag):CommonResult.failed();
    }

}

